home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / vol16n13.zip / OPENTR.ZIP / OT_SRC.ZIP / OTGLOBAL.CPP < prev    next >
C/C++ Source or Header  |  1997-05-26  |  4KB  |  95 lines

  1. // OTglobal.cpp
  2. // Global functions and data.
  3. //
  4. // OpenTrap Version 1.00 by Gregory A. Wolking
  5. // Copyright ⌐ 1997 Ziff-Davis Publishing
  6. // First published in PC Magazine, US Edition, July 1997.
  7.  
  8. #include "stdafx.h"
  9. #include <sys\timeb.h>
  10. #include <time.h>
  11. #include <process.h>
  12. #include "OTglobal.h"
  13.  
  14. #define WM_MY_STOP_LOG    (WM_USER + 1)
  15.  
  16. DWORD  __stdcall do_getcalls()
  17. {
  18.  
  19.    //Set up the callback with the VxD (note: VxD will get the
  20.    //id for this thread and use it and the callback address to
  21.    //inform us about file opens)
  22.     DeviceIoControl(g_hVXD, VXD_REGISTER_CB, &g_tcCond1,
  23.                    (sizeof(struct trap_criteria)),&g_dwCondition1,
  24.                    sizeof(g_dwCondition1), NULL, NULL );
  25.     // If user is only logging file open events, there's no need
  26.     // to establish the second trap condition record.
  27.     if (!g_bLogOpensOnly)
  28.         DeviceIoControl(g_hVXD, VXD_REGISTER_CB, &g_tcCond2,
  29.                        (sizeof(struct trap_criteria)),&g_dwCondition2,
  30.                        sizeof(g_dwCondition2), NULL, NULL ); 
  31.     // Now just wait until the g_hStopFlag event is signaled.
  32.     while( WaitForSingleObjectEx(g_hStopFlag, INFINITE, TRUE) == WAIT_IO_COMPLETION);
  33.     return 0;
  34. }
  35.  
  36. void __stdcall our_callback(DWORD trap_rec_ptr)
  37. {
  38.     append_log_record((struct trap_record *) trap_rec_ptr);
  39.     release_rec(trap_rec_ptr);
  40. }
  41.  
  42. // Appends one trap record to the buffer, packing the data to reduce the storage required.
  43. // Record is packed by copying static elements of the trap_record structure (minus the two
  44. // 256-byte string buffers). The first string buffer is stored as an ASCIIZ string following
  45. // the packed structure.
  46. // The records are stored as a doubly-linked list; since each record is of variable length,
  47. // we need to be able to walk the list in both directions while the log is stored in memory.
  48. // Note that the global variable g_pLastRecord is always maintained to point
  49. // to the last record in the file unless the file is empty, in which
  50. // case it is NULL.
  51. void append_log_record (struct trap_record * tr_ptr)
  52. {
  53.     static packed_record pr;
  54.     int len;
  55.  
  56.     if (g_bLogErrorsOnly)                    // Logging errors only?
  57.         if (tr_ptr->tr_error == 0)            // Was operation successful?
  58.             return;                            // Nothing to do if so.
  59.     if (g_bLogFull)                            // Is buffer full?
  60.         return;                                // Nothing to do if so.
  61.     EnterCriticalSection(&g_csCritical);
  62.     _ftime(&pr.pr_time);                    // Get current time.
  63.     pr.prev_record = g_pLastRecord;            // Set previous record pointer.
  64.     if (g_pLastRecord != NULL)                // If not null, set that record's next record pointer.
  65.         g_pLastRecord->next_record = (packed_record *) g_pNextRec;
  66.     g_pLastRecord = (packed_record *) g_pNextRec;    // Save pointer to this record.
  67.     pr.next_record = NULL;                    // There is no next record after this one.
  68.     ++g_intRecCount;                        // Bump record count.
  69.     pr.pr_function = tr_ptr->tr_function;    // Transfer data to packed record structure.
  70.     pr.pr_handle = tr_ptr->tr_handle;
  71.     memcpy(pr.pr_program, tr_ptr->tr_program, 9);
  72.     pr.pr_error = tr_ptr->tr_error;
  73.     pr.pr_file1 = (g_pNextRec + sizeof(pr));    // Calculate postion of filename string and set pointer.
  74.     memcpy(g_pNextRec, &pr, sizeof(pr));        // Copy packed record to buffer.
  75.     len = strlen(tr_ptr->tr_file1) + 1;            // Get length of filename string, including terminating null.
  76.     if (lstrcpy(pr.pr_file1, tr_ptr->tr_file1) == NULL)    // Copy string to buffer.
  77.     {    // Write empty string if copy failed.
  78.         len = 1;
  79.         *pr.pr_file1 = '\0';
  80.     }
  81.     g_pNextRec = pr.pr_file1 + len;        // Calculate position for next record.
  82.     if (g_pNextRec > g_pBufSafeEnd)        // If it's past the safe endpoint,
  83.     {
  84.         g_bLogFull = TRUE;                // Set full flag
  85.         PostMessage(g_hWndMainFrame, WM_MY_STOP_LOG, 0, 0);        // Tell main process that buffer is full.
  86.     }
  87.     LeaveCriticalSection(&g_csCritical);    // And we're outta here.
  88. }
  89.  
  90. void release_rec(DWORD rec_address)
  91. {
  92.     DeviceIoControl(g_hVXD, VXD_REC_DONE,
  93.         (void *) &rec_address, 4, NULL, 0, NULL, NULL );
  94. }
  95.